package com.yuncheng.oaApi.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.OaBgkcjl;
import com.yuncheng.oaApi.service.IOaBgkcjlService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.vo.zcmlVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: oa_bgkcjl
 * @Author: oabg
 * @Date: 2024-05-20
 * @Version: V1.0
 */
@Api(tags = "oa_bgkcjl接口")
@RestController
@RequestMapping("/api/spzx-oa/oaBgkcjl")
public class OaBgkcjlController extends BaseController<OaBgkcjl, IOaBgkcjlService> {

    private static final Logger log = LoggerFactory.getLogger(OaBgkcjlController.class);

    @Resource
    private IOaBgkcjlService oaBgkcjlService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    /**
     * 分页列表查询
     *
     * @param oaBgkcjl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-分页列表查询")
    @ApiOperation(value = "oa_bgkcjl-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaBgkcjl oaBgkcjl, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaBgkcjl> queryWrapper = QueryBuilder.initQueryWrapper(oaBgkcjl, req.getParameterMap());
        Page<OaBgkcjl> page = new Page<>(pageNo, pageSize);
        IPage<OaBgkcjl> pageList = oaBgkcjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    /**
     * 目录列表
     */
    @AutoLog(value = "目录列表")
    @ApiOperation(value = "目录列表")
    @GetMapping(value = "/mllist")
    public HttpResult<?> mllist() {
        //查所有
        List<OaBgkcjl> orgList = oaBgkcjlService.list(new LambdaQueryWrapper<OaBgkcjl>()
                .orderByAsc(OaBgkcjl::getCreateTime));
        List<zcmlVo> zykVOS = new ArrayList();

        orgList.forEach( bean -> {
            zcmlVo zykVo = new zcmlVo();
            BeanUtil.copyProperties(bean,zykVo);
            zykVOS.add(zykVo);
        });
        for (zcmlVo zyk : zykVOS) {
            List<zcmlVo> collect = zykVOS.stream().filter(f -> zyk.getId().equals(f.getFfjid())).collect(Collectors.toList());
            zyk.setChildren(collect);
        }

        List<zcmlVo> collect = zykVOS.stream().filter(f -> f.getFfjid() == null).collect(Collectors.toList());

        Map map = new HashMap();
        map.put("records",collect);

        return HttpResult.ok(map);
    }




    /**
     * 添加
     *
     * @param oaBgkcjl
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-添加")
    @ApiOperation(value = "oa_bgkcjl-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaBgkcjl oaBgkcjl) {
        HttpResult<String> result = new HttpResult(true);
        oaBgkcjlService.save(oaBgkcjl);
        result.setResult(oaBgkcjl.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param oaBgkcjl
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-编辑")
    @ApiOperation(value = "oa_bgkcjl-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaBgkcjl oaBgkcjl) {
        oaBgkcjlService.updateById(oaBgkcjl);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-通过id删除")
    @ApiOperation(value = "oa_bgkcjl-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        oaBgkcjlService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-批量删除")
    @ApiOperation(value = "oa_bgkcjl-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        oaBgkcjlService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "oa_bgkcjl-通过id查询")
    @ApiOperation(value = "oa_bgkcjl-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaBgkcjl oaBgkcjl = oaBgkcjlService.getById(id);
        return HttpResult.ok(oaBgkcjl);
    }

}
